package edu.gduf.controller;
/*
@author qw
@date 2020/10/11 - 0:22
**/

import edu.gduf.domain.Cooperation;
import edu.gduf.domain.ResultInfo;
import edu.gduf.service.impl.CooperationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * 组队模块的控制类
 */
@Controller
@RequestMapping("/cooperation")
public class CooperationController {

    @Autowired
    private CooperationServiceImpl cooperationService;

    /**
     * 跳转到广场页面
     *
     * @return
     */
    @RequestMapping("/cooperationSquare")
    public String Cooperation(Model model) {
        ResultInfo resultInfo = cooperationService.selectCooperationList(1);
        model.addAttribute("resultInfo", resultInfo);
        model.addAttribute("currentPageIndex", 1);
        model.addAttribute("url","/cooperation/selectAll?currentPageIndex=");
        return "cooperation/cooperationSquare";
    }

    /**
     * 预发布 跳转到填些发布信息页面
     * @return
     */
    @RequestMapping("/prePublish")
    public String prePublish(){
        return "cooperation/publishCooperation";
    }

    /**
     * 组队信息发布
     * @param cooperation 前端表单提交cooperation数据
     * @param httpSession httpSession域
     * @param model 将操作结果传至前端页面
     * @return 发布结果信息和最新的组队广场页面
     */
    @RequestMapping("/publish")
    public String publish(Cooperation cooperation, HttpSession httpSession, Model model) {
        ResultInfo resultInfo = cooperationService.addCooperation(cooperation, httpSession);
        model.addAttribute("publish_msg", resultInfo.getMessage());
        return "redirect:/cooperation/cooperationSquare";
    }

    /**
     * 组队信息删除
     * @param coid 组队信息主键coid
     * @param model 将操作结果传至前端页面
     * @return 个人查看“我的发布”页面
     */
    @RequestMapping("/delete")
    public String delete(Integer coid, Model model, RedirectAttributes attributes) {
        ResultInfo resultInfo = cooperationService.deleteCooperation(coid);
        model.addAttribute("resultInfo", resultInfo);
        attributes.addAttribute("currentPageIndex", 1);
        return "redirect:/cooperation/checkPublish";
    }

    /**
     * 组队信息批量删除
     * @param coidArray 选中复选框由信息主键组成的数组
     * @param model 将操作结果传至前端页面
     * @return 个人查看“我的发布”页面
     */
    @RequestMapping("/batchDelete")
    public String batchDelete(Integer[] coidArray, Model model) {
        ResultInfo resultInfo = cooperationService.BatchDelete(coidArray);
        model.addAttribute("resultInfo", resultInfo);
        return "user/showrelease";
    }

    /**
     * 预更新
     *
     * @return
     */
    @RequestMapping(value = "/preUpdate",method = RequestMethod.GET)
    public String preUpdate(Integer coid, Model model) {
        Cooperation cooperation = cooperationService.selectCooperationByCoid(coid);
        model.addAttribute("cooperation", cooperation);
        return "cooperation/updateCooperation";
    }

    /**
     * 组队信息更新
     * @param cooperation 获取需要修改的cooperation对象
     * @param model 将操作结果传至前端页面
     * @return 更新后的组队信息详情页
     */
    @RequestMapping("/updateCooperation")
    public String update(Cooperation cooperation, Model model) {
        ResultInfo resultInfo = cooperationService.updateCooperation(cooperation);
        model.addAttribute("resultInfo", resultInfo);
        return "cooperation/cooperationContent";
    }

    /**
     * 组队信息查询（单条）
     * @param coid 组队信息表主键
     * @return 显示详细的单条组队信息内容
     */
    @RequestMapping("/selectByCoid")
    public String selectByCoid(Integer coid, Model model) {
        Cooperation cooperation = cooperationService.selectCooperationByCoid(coid);
        //获取组队信息的作者 主键uid
        Integer uid = cooperation.getUser().getUid();
        //查询统计该作者的总发布数
        Integer uidCount = cooperationService.countByAuthor(uid);

        if (cooperation == null) {
            model.addAttribute("errorMessage", "查询失败，请检查输入数据是否有误。");
        } else {
            model.addAttribute("cooperation", cooperation);
            model.addAttribute("uidCount", uidCount);
        }
        return "cooperation/cooperationContent";
    }


    /**
     *  组队信息模糊查询（条件：标题或要求）
     * @param str 查询条件
     * @param currentPageIndex 当前页面下标值
     * @param model 将操作结果返回至前端页面
     * @return 符合条件的数据分页展示页面
     */
    @RequestMapping("/searchCooperation")
    public String searchCooperation(String str, Integer currentPageIndex, Model model) {
        ResultInfo resultInfo = cooperationService.selectCooperationByCondition(str, currentPageIndex);
        model.addAttribute("resultInfo", resultInfo);
        return "cooperation/cooperationSquare";
    }

    /**
     * 组队信息查询（全部-分页）
     * @param currentPageIndex 当前页数下标
     * @return 对应数据展示页面
     */
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    public String selectAll(Integer currentPageIndex, Model model) {
        ResultInfo resultInfo = cooperationService.selectCooperationList(currentPageIndex);
        model.addAttribute("resultInfo", resultInfo);
        model.addAttribute("currentPageIndex", currentPageIndex);
        model.addAttribute("url","/cooperation/selectAll?currentPageIndex=");
        return "cooperation/cooperationSquare";
    }

    /**
     * 组队信息查询（根据分类查询）
     * @param cid 查询条件（分类表中的主键id）
     * @return json
     */
    @RequestMapping(value = "/selectByCid",method = RequestMethod.GET)
    public String selectByCid(Integer cid, Integer currentPageIndex, Model model) {
        ResultInfo resultInfo = cooperationService.selectCooperationByCid(cid, currentPageIndex);
        model.addAttribute("resultInfo", resultInfo);
        model.addAttribute("cid", cid);
        model.addAttribute("currentPageIndex", currentPageIndex);
        model.addAttribute("url","/cooperation/selectByCid?");
        return "cooperation/cooperationSquare";
    }

    /**
     * 更新组队状态（根据组队的截止时间 判断是否停止组队 进行状态修改）
     * @param timeout 时间标识符
     * @param coid 组队信息编号（主键）
     * @return json
     */
    @ResponseBody
    @RequestMapping("/isTimeout")
    public ResultInfo updateStatus(Boolean timeout,Integer coid) {
        return cooperationService.updateStatus(timeout, coid);
    }

    /**
     * 更新组队人数
     * @param coid 组队信息编号（主键）
     * @return json
     */
    @ResponseBody
    @RequestMapping("/updatePopulation")
    public ResultInfo updatePopulation(Integer coid, String username) {
        return cooperationService.updatePopulation(coid, username);
    }


    /**
     * 查看我的发布
     * @param session
     * @param currentPageIndex
     * @param model
     * @return
     */
    @RequestMapping(value = "/checkPublish",method = RequestMethod.GET)
    public String checkPublish(HttpSession session, Integer currentPageIndex, Model model) {
        ResultInfo resultInfo = cooperationService.selectCooperationByUid(session,currentPageIndex);
        model.addAttribute("resultInfo", resultInfo);
        model.addAttribute("currentPageIndex", currentPageIndex);
        model.addAttribute("url","/cooperation/checkPublish?");
        return "user/showrelease";
    }


    @RequestMapping("/cooperationContent")
    public String detail() {
        return "cooperation/cooperationContent";
    }
}
